<?php


namespace app\api\service;


use app\lib\exception\checkSensitiveException;

class ValidataSensitive{

    public function checkData($content){
        $appid = config('wx.appId');
        $secret = config('wx.secretId');
        $access_token = $this->getToken($appid,$secret)['access_token'];
        $content = '{ "content":"'.$content.'" }';//重点是，发送的数据的比对，一般是json格式的，要按照实际的情况来定
        $result = $this->sendData($access_token,$content);

        return $result;


        return true;
    }

    //获取token
    private function getToken($appid,$secret){
        $baseUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
        $url = sprintf($baseUrl,$appid,$secret);
        $result = json_decode(curl_get($url),true);
        if(array_key_exists('errcode',$result)){
             throw new checkSensitiveException(['errcode'=>$result['errcode'],'msg'=>$result['errmsg']]);
        }
        return $result;
    }

    //词汇验证
    private function sendData($access_token,$data){
        $baseUrl = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=%s';
        $url = sprintf($baseUrl,$access_token);
        $result = json_decode(curl_post($url,$data),true);
        return  $result;
    }


    //图片验证
    private function sendImg($access_token,$img){
        $baseUrl = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=%s';
        $url = sprintf($baseUrl,$access_token);
        $result = json_decode(curl_post($url,$img),true);
        return  $result;
    }

    //获取图片路径
    private  function getImgStyle($img){
        $image = getPrefix().'/'.substr($img,strpos($img,'attachment'));
        if (class_exists('\CURLFile')) {
            $imgData = array('media' => new \CURLFile(realpath($image)));
        } else {
            $imgData = array('media' => '@' . realpath($image));
        }
        return $imgData;
    }












}




